<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据日期" prop="vouchdate">
        <el-input
          v-model="queryParams.vouchdate"
          placeholder="请输入单据日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经办人" prop="vhandlepsnName">
        <el-input
          v-model="queryParams.vhandlepsnName"
          placeholder="请输入经办人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预算项目" prop="loanBillDcsYjyszxmtz001">
        <el-input
          v-model="queryParams.loanBillDcsYjyszxmtz001"
          placeholder="请输入预算项目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资金来源" prop="loanBillDcsYjytzzdy0001">
        <el-input
          v-model="queryParams.loanBillDcsYjytzzdy0001"
          placeholder="请输入资金来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用承担部门" prop="vfinacedeptidName">
        <el-input
          v-model="queryParams.vfinacedeptidName"
          placeholder="请输入费用承担部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="pkCusdocName">
        <el-input
          v-model="queryParams.pkCusdocName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预付金额" prop="nloanmny">
        <el-input
          v-model="queryParams.nloanmny"
          placeholder="请输入预付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

         <el-form-item label="审批状态" prop="status">
       <el-select v-model="queryParams.status" placeholder="请选择审批状态" clearable>
                    <el-option
                      v-for="dict in dict.type.yy_bip_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sci:prepayment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sci:prepayment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sci:prepayment:remove']"
        >删除</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sci:prepayment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prepaymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据日期" align="center" prop="vouchdate" />
      <el-table-column label="经办人" align="center" prop="vhandlepsnName" />
      <el-table-column label="预算项目" align="center" prop="loanBillDcsYjyszxmtz001" />
      <el-table-column label="资金来源" align="center" prop="loanBillDcsYjytzzdy0001" />
      <el-table-column label="费用承担部门" align="center" prop="vfinacedeptidName" />
      <el-table-column label="供应商名称" align="center" prop="pkCusdocName" />
      <el-table-column label="预付金额" align="center" prop="nloanmny" />
      <el-table-column label="付款状态" align="center" prop="status" >
       <template slot-scope="scope">
            <dict-tag :options="dict.type.yy_bip_status" :value="scope.row.status"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sci:prepayment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sci:prepayment:remove']"
          >删除</el-button> -->
             <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['sci:prepayment:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对公预付单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="单据日期" prop="vouchdate">
          <el-input v-model="form.vouchdate" placeholder="请输入单据日期" />
        </el-form-item>
        <el-form-item label="费用承担组织" prop="cfinaceorgName">
          <el-input v-model="form.cfinaceorgName" placeholder="请输入费用承担组织" />
        </el-form-item>
        <el-form-item label="经办人" prop="vhandlepsnName">
          <el-input v-model="form.vhandlepsnName" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="预算项目" prop="loanBillDcsYjyszxmtz001">
          <el-input v-model="form.loanBillDcsYjyszxmtz001" placeholder="请输入预算项目" />
        </el-form-item>
        <el-form-item label="资金来源" prop="loanBillDcsYjytzzdy0001">
          <el-input v-model="form.loanBillDcsYjytzzdy0001" placeholder="请输入资金来源" />
        </el-form-item>
        <el-form-item label="附件数" prop="loanBillDcsYjyfjzstz0001">
          <el-input v-model="form.loanBillDcsYjyfjzstz0001" placeholder="请输入附件数" />
        </el-form-item>
        <el-form-item label="京东订单号" prop="vmemo">
          <el-input v-model="form.vmemo" placeholder="请输入京东订单号" />
        </el-form-item>
        <el-form-item label="付款说明" prop="vreason">
          <el-input v-model="form.vreason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="费用承担部门" prop="vfinacedeptidName">
          <el-input v-model="form.vfinacedeptidName" placeholder="请输入费用承担部门" />
        </el-form-item>
        <el-form-item label="费用项目名称" prop="loanbillbvosPkBusimemoName">
          <el-input v-model="form.loanbillbvosPkBusimemoName" placeholder="请输入费用项目名称" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="pkCusdocName">
          <el-input v-model="form.pkCusdocName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="预付金额" prop="nloanmny">
          <el-input v-model="form.nloanmny" placeholder="请输入预付金额" />
        </el-form-item>
        <el-form-item label="合同名称" prop="pkApctName">
          <el-input v-model="form.pkApctName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="合同总额" prop="ndefnum1">
          <el-input v-model="form.ndefnum1" placeholder="请输入合同总额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看抽屉 -->
    <el-drawer
      title="预付单详情"
      :visible.sync="drawer"
      size="50%"
      direction="rtl"
    >

      <!-- 自定义标题插槽 -->
  <div slot="title" class="drawer-title">
    <span>采购需求详情</span>
    <el-tag 
      :type="getStatusTagType(formView.status)"
      size="medium"
      style="margin-left: 10px;"
    >
      <dict-tag 
        :options="dict.type.yy_bip_status" 
        :value="formView.status"
      />
    </el-tag>
  </div>
      <!-- 第一部分 - 指定字段分组 -->
      <el-card shadow="always" style="margin: 20px;">
        <el-descriptions title="基本信息" :column="2" border>
           <el-descriptions-item label="单据编号">{{ formView.code }}</el-descriptions-item>
          <el-descriptions-item label="单据日期">{{ formView.vouchdate }}</el-descriptions-item>
          <el-descriptions-item label="费用承担组织">{{ formView.cfinaceorgName }}</el-descriptions-item>
          <el-descriptions-item label="经办人">{{ formView.vhandlepsnName }}</el-descriptions-item>
          <el-descriptions-item label="预算项目">{{ formView.loanBillDcsYjyszxmtz001 }}</el-descriptions-item>
          <el-descriptions-item label="资金来源">{{ formView.loanBillDcsYjytzzdy0001 }}</el-descriptions-item>
          <el-descriptions-item label="附件数">{{ formView.loanBillDcsYjyfjzstz0001 }}</el-descriptions-item>
          <el-descriptions-item label="京东订单号">{{ formView.vmemo }}</el-descriptions-item>
          <el-descriptions-item label="付款说明">{{ formView.vreason }}</el-descriptions-item>
        </el-descriptions>
      </el-card>

      <!-- 第二部分 - 其余字段 -->
      <el-card shadow="always" style="margin: 20px;">
        <el-descriptions title="预付款明细" :column="2" border>
           <el-descriptions-item label="费用承担部门">{{ formView.vfinacedeptidName }}</el-descriptions-item>
            <el-descriptions-item label="费用项目名称">{{ formView.loanbillbvosPkBusimemoName }}</el-descriptions-item>
          <el-descriptions-item label="供应商名称">{{ formView.pkCusdocName }}</el-descriptions-item>
          <el-descriptions-item label="预付金额">{{ formView.nloanmny }}</el-descriptions-item>
          <el-descriptions-item label="合同名称">{{ formView.pkApctName }}</el-descriptions-item>
          <el-descriptions-item label="合同总额">{{ formView.ndefnum1 }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { listPrepayment, getPrepayment, delPrepayment, addPrepayment, updatePrepayment } from "@/api/sci/prepayment";

export default {
  name: "Prepayment",
  dicts:['yy_bip_status'],
  data() {
    return {
        drawer: false, // 控制抽屉显示
      formView: {},   // 查看表单数据
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对公预付单表格数据
      prepaymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vouchdate: null,
        vhandlepsnName: null,
        loanBillDcsYjyszxmtz001: null,
        loanBillDcsYjytzzdy0001: null,
        vfinacedeptidName: null,
        pkCusdocName: null,
        nloanmny: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     /** 查看按钮操作 */
    handleView(row) {
      const id = row.id || this.ids;
      getPrepayment(id).then(response => {
        this.formView = response.data;
        this.drawer = true;
      });
    },
    /** 查询对公预付单列表 */
    getList() {
      this.loading = true;
      listPrepayment(this.queryParams).then(response => {
        this.prepaymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        vouchdate: null,
        cfinaceorgName: null,
        vhandlepsnName: null,
        loanBillDcsYjyszxmtz001: null,
        loanBillDcsYjytzzdy0001: null,
        loanBillDcsYjyfjzstz0001: null,
        vmemo: null,
        vreason: null,
        vfinacedeptidName: null,
        loanbillbvosPkBusimemoName: null,
        pkCusdocName: null,
        nloanmny: null,
        pkApctName: null,
        ndefnum1: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加对公预付单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPrepayment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改对公预付单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePrepayment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrepayment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除对公预付单编号为"' + ids + '"的数据项？').then(function() {
        return delPrepayment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sci/prepayment/export', {
        ...this.queryParams
      }, `prepayment_${new Date().getTime()}.xlsx`)
    },
      getStatusTagType(status) {
    switch(status) {
      case 0: return 'info';    // 未提交
      case 3: return 'warning'; // 审批中
      case 1: return 'success'; // 审批通过
      default: return 'info';
    }
  },
  }
};
</script>
<style scoped>
/* 添加抽屉内容的样式 */
.el-drawer__body {
  padding: 0;
  overflow-y: auto;
}

.el-card {
  margin-bottom: 15px;
}

.el-descriptions__title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}
</style>